from unit_test import \
    assume_true_failed \
  , assume_false_failed \
  , assume_equal_failed \
  , assume_not_equal_failed \
  , assume_equal_len_failed \
  , assume_not_equal_len_failed \
  , assume_empty_failed \
  , assume_not_empty_failed \
  , assume_is_str_failed \
  , test_suite

def check_assumption_passed ( assumption ) :
    class checker :
        def __init__ ( self , assumption ) :
            self . _assumption = assumption
        def invoke ( self , * args ) :
            self . _assumption ( * args )
    return checker ( assumption ) . invoke

def check_assumption_failed ( assumption , exception ) :
    class checker :
        def __init__ ( self , assumption ) :
            self . _assumption = assumption
        def invoke ( self , * args ) :
            try :
                self . _assumption ( * args )
                assert False
            except exception :
                pass
    return checker ( assumption ) . invoke

class suite_fake :
    _call_history = [ ]
    def __init__ ( self , test ) :
        suite_fake . _call_history += [ suite_fake . __init__ ]
    def __del__ ( self ) :
        suite_fake . _call_history += [ suite_fake . __del__ ]
    def test_first ( self ) :
        suite_fake . _call_history += [ suite_fake . test_first ]
    def test_second ( self ) :
        suite_fake . _call_history += [ suite_fake . test_second ]

class unit_test_tests :
    'unit_test'
    def __init__ ( self , test ) :
        self . _test = test
    def test_assume_true ( self ) :
        check_assumption_passed ( self . _test . assume_true ) ( True )
        check_assumption_failed ( self . _test . assume_true , assume_true_failed ) ( False )
    def test_assume_false ( self ) :
        check_assumption_passed ( self . _test . assume_false ) ( False )
        check_assumption_failed ( self . _test . assume_false , assume_false_failed ) ( True )
    def test_assume_equal ( self ) :
        check_assumption_passed ( self . _test . assume_equal ) ( 5 , 5 )
        check_assumption_failed ( self . _test . assume_equal , assume_equal_failed ) ( 4 , 5 )
    def test_assume_not_equal ( self ) :
        check_assumption_passed ( self . _test . assume_not_equal ) ( 6 , 7 )
        check_assumption_failed ( self . _test . assume_not_equal , assume_not_equal_failed ) ( 5 , 5 )
    def test_assume_equal_len ( self ) :
        check_assumption_passed ( self . _test . assume_equal_len ) ( [ 1 , 2 , 3 ] , 3 )
        check_assumption_failed ( self . _test . assume_equal_len , assume_equal_len_failed ) ( [ 1 , 2 , 3 ] , 5 )
    def test_assume_not_equal_len ( self ) :
        check_assumption_passed ( self . _test . assume_not_equal_len ) ( [ 1 , 2 , 3 ] , 5 )
        check_assumption_failed ( self . _test . assume_not_equal_len , assume_not_equal_len_failed ) ( [ 1 , 2 , 3 ] , 3 )
    def test_assume_empty ( self ) :
        check_assumption_passed ( self . _test . assume_empty ) ( [ ] )
        check_assumption_failed ( self . _test . assume_empty , assume_empty_failed ) ( [ 1 , 2 , 3 ] )
    def test_assume_not_empty ( self ) :
        check_assumption_passed ( self . _test . assume_not_empty ) ( [ 1 , 2 , 3 ] )
        check_assumption_failed ( self . _test . assume_not_empty , assume_not_empty_failed ) ( [ ] )    
    def test_assume_is_str ( self ) :
        check_assumption_passed ( self . _test . assume_is_str ) ( 'my str' )
        check_assumption_failed ( self . _test . assume_is_str , assume_is_str_failed ) ( [ 'm' , 'y' , ' ' , 's' , 't' , 'r' ] )
    def test_suite_execution_order ( self ) :
        suite = test_suite ( suite_fake )
        suite . run ( )
        self . _test . assume_equal ( suite_fake . _call_history ,
            [ suite_fake . __init__
            , suite_fake . test_first
            , suite_fake . __del__
            , suite_fake . __init__
            , suite_fake . test_second
            , suite_fake . __del__
            ] )
